﻿@using System.Configuration
@using System.Data.SqlClient
@{
    
        
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Sync B2E Page";

    bool hasError = false;
    string errorMessage = string.Empty;
    bool hasSuccess = false;
    string successMessage = string.Empty;

    System.Data.DataSet ds = new System.Data.DataSet("CurrentClaims");
    
    
    if (IsPost)
    {
        AddTask("DoWork", 60);
            
        
        System.Collections.IDictionaryEnumerator e = HttpRuntime.Cache.GetEnumerator();
        while (e.MoveNext())
        {
            object o = e.Current;  
        }
        
    } // if (IsPost)
    
}


<div class="container-fluid main-content" role="main">

    <h2>
        Sync B2E
    </h2>
    
    <div class="panel panel-default">
        <div class="panel-heading">Instructions (How should this page work?)</div>
        <div class="panel-body">
            <p>
                This page is for users to sync B2E intranet ID and full name.<br />
            </p>

            <ul>
                <li>HR can click on Sync B2E button update records.</li>
            </ul>
        </div>
    </div>

    @if (hasError)
    {
    <div class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Warning!</strong> @errorMessage
    </div>
    }
    @if (hasSuccess)
    {
    <div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Success!</strong> @Html.Raw(successMessage)
    </div>
    }

    <form method="post">

        <button type="submit" name="actionButton" value="update" class="btn btn-primary">
            <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>
            Sync B2E
        </button>

        @Html.Raw(successMessage)
        
    </form>

</div>

@section Scripts
{
    <script type="text/javascript" src="~/Scripts/kendo/2014.3.1119/kendo.ui.core.min.js"></script>
    <script type="text/javascript" src="~/Scripts/moment.js"></script>
    <script type="text/javascript" src="~/Scripts/moment-with-locales.js"></script>
    <script type="text/javascript">
    (function ($) {
        "use strict";
    }(jQuery));
    </script>
}

@functions {
    //private static CacheItemRemovedCallback OnCacheRemove = null;

    private void AddTask(string name, int seconds)
    {
        ApplicationStatus("Do AddTask2 task at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
        
        //OnCacheRemove = new System.Web.Caching.CacheItemRemovedCallback(CacheItemRemoved);
        if (KeppelLand.Services.RecurringJobHost.OnCacheRemove == null)
            KeppelLand.Services.RecurringJobHost.OnCacheRemove = new System.Web.Caching.CacheItemRemovedCallback(CacheItemRemoved);

        if (HttpRuntime.Cache[name] == null)
            HttpRuntime.Cache.Insert(name, seconds, null,
                DateTime.Now.AddSeconds(seconds), System.Web.Caching.Cache.NoSlidingExpiration,
                System.Web.Caching.CacheItemPriority.NotRemovable,
                KeppelLand.Services.RecurringJobHost.OnCacheRemove);
    }

    public void CacheItemRemoved(string k, object v, System.Web.Caching.CacheItemRemovedReason r)
    {
        //System.DateTime.Now.DayOfWeek
        
        // do stuff here if it matches our taskname, like WebRequest
        ApplicationStatus("Do CachedItemRemoved2 task at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
        
        // re-add our task so it recurs
        AddTask(k, Convert.ToInt32(v));
    }

    public void ApplicationStatus(string statusMessage)
    {
        string logsPath = Server.MapPath("~/App_Data/Logs");
        if (!System.IO.Directory.Exists(logsPath))
            System.IO.Directory.CreateDirectory(logsPath);

        string logFileName = string.Format("Application_StartEnd.log");
        string logFilePath = System.IO.Path.Combine(logsPath, logFileName);

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(statusMessage);

        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(logFilePath, true))
        {
            sw.AutoFlush = true;

            sw.WriteLine(sb.ToString());

            sw.WriteLine("\tSending application status email alert:[START  : {0}]", DateTime.Now.ToString("yyyy-MM-dd, HH:mm:ss"));

            KeppelLand.Helpers.EmailHelper.SendStartEndAlertEmail(sb.ToString());

            sw.WriteLine("\tSending application status email alert:[END    : {0}]\n", DateTime.Now.ToString("yyyy-MM-dd, HH:mm:ss"));
            sw.WriteLine("--------------------------------------------------------------------------------");
        }
    }

}
